
package org.apache.solr.logging;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.core.CoreContainer;

/**
 * A Class to monitor Logging events and hold N events in memory
 * This is abstract so we can support both JUL and Log4j (and other logging platforms)
 */
public abstract class LogWatcher<E> {

    protected CircularList<E> history;
    protected long last = -1;

    /**
     * @return The implementation name
     */
    public abstract String getName();

    /**
     * @return The valid level names for this framework
     */
    public abstract List<String> getAllLevels();

    /**
     * Sets the log level within this framework
     */
    public abstract void setLogLevel(String category, String level);

    /**
     * @return all registered loggers
     */
    public abstract Collection<LoggerInfo> getAllLoggers();

    public abstract void setThreshold(String level);

    public abstract String getThreshold();

    public void add(E event, long timstamp) {
        history.add(event);
        last = timstamp;
    }

    public long getLastEvent() {
        return last;
    }

    public int getHistorySize() {
        return (history == null) ? -1 : history.getBufferSize();
    }

    public SolrDocumentList getHistory(long since, AtomicBoolean found) {
        if(history == null) {
            return null;
        }

        SolrDocumentList docs = new SolrDocumentList();
        Iterator<E> iter = history.iterator();
        while(iter.hasNext()) {
            E e = iter.next();
            long ts = getTimestamp(e);
            if(ts == since) {
                if(found != null) {
                    found.set(true);
                }
            }
            if(ts > since) {
                docs.add(toSolrDocument(e));
            }
        }
        docs.setNumFound(docs.size()); // make it not look too funny
        return docs;
    }

    public abstract long getTimestamp(E event);

    public abstract SolrDocument toSolrDocument(E event);

    public abstract void registerListener(ListenerConfig cfg, CoreContainer container);

    public void reset() {
        history.clear();
        last = -1;
    }
}
